export default {
  data() {
    return {
      addRoleForm: [],
      addRoleDialogVisible: false,
      addRoleRuleForm: {},
      addRoleRules: {
        roleName: [{ required: true, message: '请填写角色名称', trigger: 'blur' }],
        roleDesc: [{ required: true, message: '请填写角色描述', trigger: 'blur' }]
      },
      editRoleDialogVisible: false,
      editRoleRuleForm: {},
      editRoleRules: {
        roleName: [{ required: true, message: '请填写角色名称', trigger: 'blur' }],
        roleDesc: [{ required: true, message: '请填写角色描述', trigger: 'blur' }]
      },
      setRightDialogVisible: false,
      rightTree: [],
      treeConfig: {
        children: 'children',
        label: 'authName'
      },
      defaultCheckedKeys: [],
      selectRoleId: null
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) return this.$message.error('获取用户列表失败!')
      this.addRoleForm = res.data
    },
    // 重置添加表单
    resetAddForm() {
      this.$refs.addRoleFormRef.resetFields()
    },
    // 添加角色
    addRole() {
      this.$refs.addRoleFormRef.validate(async valid => {
        if (!valid) return
        // 存入
        const { data: res } = await this.$http.post('roles', this.addRoleRuleForm)
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.addRoleDialogVisible = false
        this.getRoleList()
      })
    },
    // 重置个人角色表单
    editResetForm() {
      this.editRoleRuleForm = {}
      this.$refs.editUserFormRef.resetFields()
      this.editRoleDialogVisible = false
    },
    // 获取个人角色表单
    async editFrom(id) {
      const { data: res } = await this.$http.get(`roles/${id}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.editRoleRuleForm = res.data
      this.editRoleDialogVisible = true
    },
    // 保存编辑个人角色
    saveRole(id) {
      this.$refs.editUserFormRef.validate(async valid => {
        if (!valid) return
        // 存入
        const { data: res } = await this.$http.put(`roles/${id}`, {
          roleName: this.editRoleRuleForm.roleName,
          roleDesc: this.editRoleRuleForm.roleName
        })
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.editRoleDialogVisible = false
        this.getRoleList()
      })
    },
    // 移除个人用户信息
    removeForm(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const { data: res } = await this.$http.delete(`roles/${id}`)
          if (res.meta.status !== 200) return this.$message.error('删除用户角色失败')
          this.$message.success(res.meta.msg)
          this.getRoleList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 删除角色权限
    async removeRight(role, id) {
      const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${id}`)
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      role.children = res.data
    },
    // 分配权限
    async showSetRightDialog(role) {
      this.setRightDialogVisible = true
      // 在展示对话框之前，先获取到 权限的树形结构的数据
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) return this.$message.error('初始化权限失败！')
      // 把权限的树形结构数据，保存到data中，供页面渲染使用
      this.rightTree = res.data
      const keys = [] // 专门存放所有三级节点的Id
      this.getLeafIds(role, keys)
      this.selectedRoleId = role.id
      this.defaultCheckedKeys = keys
    },
    // 根据指定的节点和keys数组，递归获取所有三级节点的Id
    getLeafIds(node, keys) {
      if (!node.children) {
        keys.push(node.id)
      } else {
        node.children.forEach(item => this.getLeafIds(item, keys))
      }
    },
    // 重置分配角色
    resetSetRightDialog() {
      this.selectedRoleId = null
      this.rightTree = []
      this.defaultCheckedKeys = []
    },
    // 保存分配角色
    async saveRight() {
      // 1. 获取树形控件中，所有半选和全选节点的Id数组
      const arr1 = this.$refs.tree.getCheckedKeys()
      const arr2 = this.$refs.tree.getHalfCheckedKeys()
      const rids = [...arr1, ...arr2].join(',')
      const { data: res } = await this.$http.post(`roles/${this.selectedRoleId}/rights`, { rids })
      if (res.meta.status !== 200) return this.$message.error('分配权限失败！')
      this.$message.success('分配权限成功！')
      this.setRightDialogVisible = false
      this.getRoleList()
    }
  }
}
